#!/bin/bash

LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
function ExitCleanup {
    # keep the log file, but ensure a different user can overwrite it next time
    cd /
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM installation task.
#-------------------------------------------------------------------------------
function InstallRpm {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$r" == "$i" ]; then
                return
            fi
        done
    fi

    CMD=`echo $1`; shift;
    RPM=`echo $1`; shift;
    OPT=`echo $*`
    if [ "$OPT" == "" ]; then
        OPT="--force --nodeps"
    fi

    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    if [ -f $RPM ]; then
        echo "=====================================================" >> $LogFile
        echo "***** Executing rpm -vv $CMD $RPM $OPT *****         " >> $LogFile
        echo "=====================================================" >> $LogFile
        rpm -vv $CMD $RPM $OPT >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo "Error installing rpm fileset named $2" >> $LogFile
            if [ "$Update" == "true" ]; then
                echo "Error installing rpm fileset named $2"
                ExitCleanup 9
            fi
        fi
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM removal task.
#-------------------------------------------------------------------------------
function EraseRpm {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$x" == "$i" ]; then
                return
            fi
        done
    fi
    echo "=====================================================" >> $LogFile
    echo "***** Executing rpm -evv $* *****                    " >> $LogFile
    echo "=====================================================" >> $LogFile
    rpm -evv $* --nodeps --allmatches >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        echo "Error removing rpm fileset named $2" >> $LogFile
    fi
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" == "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    ExitCleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | \
        xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]; then
    ln -sf /opt/IBMJava2-142 /opt/IBMJava
fi

# Remove the zip file to save space
rm -f /usr/local/hsc_install.images/*.zip

VER=`/opt/hsc/bin/hsc version | grep Version | cut -d':' -f2 |awk '{print $1}'`
REL=`/opt/hsc/bin/hsc version | grep Release | cut -d':' -f2`
REL=`echo $REL | cut -d'.' -f1|awk '{print $1}'`

if [[ "$VER" == "4"  &&  "$REL" == "4" ]]; then
    cd /
    rm -f /tmp/WsmSaved.tar
    /bin/tar cf /tmp/WsmSaved.tar /var/websm/security /usr/websm/codebase/SM* \
       /var/websm/config/user_settings/websm.cfg 2>/dev/null

    # Save old log files
    if [ -f $LogFile ]; then
        mv -f ${LogFile}.3 ${LogFile}.4 2>/dev/null
        mv -f ${LogFile}.2 ${LogFile}.3 2>/dev/null
        mv -f ${LogFile}.1 ${LogFile}.2 2>/dev/null
        mv -f ${LogFile}   ${LogFile}.1 2>/dev/null
    fi

    echo "=========================================================" >> $LogFile
    echo "*********   Performing Update operation `date`  *********" >> $LogFile
    echo "=========================================================" >> $LogFile

    # Install the base HMC rpms if baseHMC directory exists
    if [ -d $image/websm ]; then
        cd $image/websm
        echo "--- Installing Websm ------"
	EraseRpm sysmgt.websm.security
	InstallRpm -ivh sysmgt.websm.security-*.rpm
	cp $image/hmcfinddev /opt/hsc/bin
	chown bin.bin /opt/hsc/bin/hmcfinddev
	chmod 555 /opt/hsc/bin/hmcfinddev
        grep -q  "MH00221: 7310CR3 external floppy fix (02-14-2005)" /opt/hsc/data/version
        if [ $? -ne 0 ]; then
            echo "MH00221: 7310CR3 external floppy fix (02-14-2005)" >> /opt/hsc/data/version
        fi
        cat /opt/hsc/data/version > /root/.version
        cat /opt/hsc/data/version > /home/hscroot/.version
        chmod 644 /home/hscroot/.version
        chown hscroot.hmc /home/hscroot/.version
	cd /
	/bin/tar -xf /tmp/WsmSaved.tar
    fi

    if [ -f $image/postinstall ]; then
        $image/postinstall
    fi

    echo "=========================================================" >> $LogFile
    echo "********* Install/Update complete at `date` *********"     >> $LogFile
    echo "=========================================================" >> $LogFile
else
    echo "---- Cannot install this fix (MH00221) on this HMC Level ---"
fi
ExitCleanup 0
